﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gaf.AspNetCore
{
    internal class FileUtil
    {
        /// <summary>
        /// 复制文件夹及文件
        /// (不包括源文件夹根目录名称, 只是复制其中内容到目标文件夹)
        /// https://www.cnblogs.com/wangjianhui008/p/3234519.html
        /// </summary>
        /// <param name="sourceFolder">原文件路径</param>
        /// <param name="destFolder">目标文件路径</param>
        /// <returns></returns>
        public static bool CopyFolder(string sourceFolder, string destFolder)
        {
            try
            {
                // 如果目标路径不存在,则创建目标路径
                if (!Directory.Exists(destFolder))
                {
                    Directory.CreateDirectory(destFolder);
                }
                // 得到原文件根目录下的所有文件
                var files = Directory.EnumerateFiles(sourceFolder);
                foreach (string file in files)
                {
                    string name = Path.GetFileName(file);
                    string dest = Path.Combine(destFolder, name);
                    File.Copy(file, dest);//复制文件
                }
                // 得到原文件根目录下的所有文件夹
                var folders = Directory.EnumerateDirectories(sourceFolder);
                foreach (string folder in folders)
                {
                    string name = Path.GetFileName(folder);
                    string dest = Path.Combine(destFolder, name);
                    CopyFolder(folder, dest);//构建目标路径,递归复制文件
                }
                return true;
            }
            catch (Exception )
            {

                return false;
            }

        }
    }
}
